package com.juyu.miliao.authentication.config;

import com.juyu.miliao.common.dataSource.MultipleDataSourceParser;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

public class IdCreater {
    private final StringRedisTemplate stringRedisTemplate;
    private final MultipleDataSourceParser multipleDataSourceParser;

    public IdCreater(StringRedisTemplate stringRedisTemplate, MultipleDataSourceParser multipleDataSourceParser){
        this.stringRedisTemplate = stringRedisTemplate;
        this.multipleDataSourceParser = multipleDataSourceParser;
    }

    public long newUserId(){
        long increment = stringRedisTemplate.opsForValue().increment("userId", 1L);
        return (increment + 100000) * 100 + increment % multipleDataSourceParser.getSubDataSourceNames("miliao-user").size();
    }

    public long newIdByUserId(long userId){
        long increment = stringRedisTemplate.opsForValue().increment("globalId", 1L);
        return increment * 100 + userId%100;
    }

}
